package com.itheima.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.mapper.CheckItemAndGroupMapper;
import com.itheima.mapper.SetmealAndCheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealAndCheckGroupMapper setmealAndCheckGroupMapper;

    @Override
    public PageResult getSetmeal(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Setmeal> list = setmealMapper.selectByCondition(queryPageBean);
        Page page = (Page<Setmeal>) list;
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public Result add(Integer[] checkGroupIds, Setmeal setmeal) {
        setmealMapper.add(setmeal);
        if (checkGroupIds != null && checkGroupIds.length > 0) {
            for (Integer checkGroupId : checkGroupIds) {
                setmealAndCheckGroupMapper.add(setmeal.getId(), checkGroupId);
            }
        }
        return new Result(true, "添加成功");
    }

    @Override
    public Result delete(Integer id) {
        setmealMapper.delete(id);
        return new Result(true, "删除成功");
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    @Override
    public Result edit(Setmeal setmeal) {
        setmealMapper.edit(setmeal);
        return new Result(true, "编辑成功");
    }
}
